/*
********************************************************************************
*
*	name : dwt delay
*	file : bsp_dwt.c
********************************************************************************
*/
#define IN_DMA_MOUDLE
#include "bsp.h"
#include "framework.h"
#include "bsp_dma.h"

/**
 * @brief  dma isr list
 */
dma_it_type dma_it;

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  DMAx stream enable callback
 * @retval None
 */
void dma_nvic_set(const app_dma_type *dmax, uint32_t irq_en, void (*callback)(void))
{
	int stream = dmax->ch_num;

	if (stream > 7)
		return;

	if (dmax->DMAx == DMA1)
	{
		nvic_irq_set(dmax->irqn, 5, irq_en);
		(&dma_it.stream10)[stream] = callback;
	}
	if (dmax->DMAx == DMA2)
	{
		nvic_irq_set(dmax->irqn, 3, irq_en);
		(&dma_it.stream20)[stream] = callback;
	}
}

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  DMAx stream cfg
 * @retval None
 */
void dma_stream_config(const app_dma_type *dmax, dma_stream_cfg_type *cfg)
{
	DMA_Config_T dma_init_struct;

	//dmamux_init(dmax->mux, dmax->trig_src);

	DMA_ConfigStructInit(&dma_init_struct);
	
	dma_init_struct.channel = dmax->trig_src;
	
	dma_init_struct.bufferSize = cfg->data_len;
	dma_init_struct.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
	dma_init_struct.memoryInc = cfg->mem_inc ? DMA_MEMORY_INC_ENABLE : DMA_MEMORY_INC_DISABLE;
	dma_init_struct.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
	dma_init_struct.peripheralInc = cfg->periph_inc ? DMA_PERIPHERAL_INC_ENABLE : DMA_PERIPHERAL_INC_DISABLE;
	dma_init_struct.priority = cfg->prio;
	dma_init_struct.loopMode = cfg->cycle_mode ? DMA_MODE_CIRCULAR : DMA_MODE_NORMAL;

	dma_init_struct.memoryBaseAddr = (uint32_t)(cfg->mem);
	dma_init_struct.peripheralBaseAddr = (uint32_t)(cfg->periph);
	dma_init_struct.dir = cfg->mem_to_periph ? DMA_DIR_MEMORYTOPERIPHERAL : DMA_DIR_PERIPHERALTOMEMORY;
	
	dma_init_struct.fifoMode = DMA_FIFOMODE_DISABLE;
	dma_init_struct.memoryBurst = DMA_MEMORYBURST_SINGLE;
	dma_init_struct.peripheralBurst = DMA_PERIPHERALBURST_SINGLE;
	
	DMA_Config(dmax->stream, &dma_init_struct);
}

/**
 * @brief  This function handles DMA1_Stream0_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR0_IRQHandler(void)
{
	if (dma_it.stream10)
		dma_it.stream10();
}


/**
 * @brief  This function handles DMA1_Stream1_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR1_IRQHandler(void)
{
	if (dma_it.stream11)
		dma_it.stream11();
}

/**
 * @brief  This function handles DMA1_Stream2_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR2_IRQHandler(void)
{
	if (dma_it.stream12)
		dma_it.stream12();
}

/**
 * @brief  This function handles DMA1_Stream3_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR3_IRQHandler(void)
{
	if (dma_it.stream13)
		dma_it.stream13();
}

/**
 * @brief  This function handles DMA1_Stream4_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR4_IRQHandler(void)
{
	if (dma_it.stream14)
		dma_it.stream14();
}

/**
 * @brief  This function handles DMA1_Stream5_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR5_IRQHandler(void)
{
	if (dma_it.stream15)
		dma_it.stream15();
}

/**
 * @brief  This function handles DMA1_Stream6_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR6_IRQHandler(void)
{
	if (dma_it.stream16)
		dma_it.stream16();
}

/**
 * @brief  This function handles DMA1_Stream7_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA1_STR7_IRQHandler(void)
{
	if (dma_it.stream17)
		dma_it.stream17();
}

/**
 * @brief  This function handles DMA2_Stream0_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR0_IRQHandler(void)
{
	if (dma_it.stream20)
		dma_it.stream20();
}

/**
 * @brief  This function handles DMA2_Stream1_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR1_IRQHandler(void)
{
	if (dma_it.stream21)
		dma_it.stream21();
}

/**
 * @brief  This function handles DMA2_Stream2_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR2_IRQHandler(void)
{
	if (dma_it.stream22)
		dma_it.stream22();
}

/**
 * @brief  This function handles DMA2_Stream3_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR3_IRQHandler(void)
{
	if (dma_it.stream23)
		dma_it.stream23();
}

/**
 * @brief  This function handles DMA2_Stream4_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR4_IRQHandler(void)
{
	if (dma_it.stream24)
		dma_it.stream24();
}

/**
 * @brief  This function handles DMA2_Stream5_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR5_IRQHandler(void)
{
	if (dma_it.stream25)
		dma_it.stream25();
}

/**
 * @brief  This function handles DMA2_Stream6_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR6_IRQHandler(void)
{
	if (dma_it.stream26)
		dma_it.stream26();
}

/**
 * @brief  This function handles DMA2_Stream7_IRQHandler exception.
 * @param  None
 * @retval None
 */
void DMA2_STR7_IRQHandler(void)
{
	if (dma_it.stream27)
		dma_it.stream27();
}
